package com.kkb.service.file.impl;

import com.kkb.bean.*;
import com.kkb.dao.*;
import com.kkb.service.file.AnthologyTableService;
import com.kkb.utils.util.MassageReturnUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 何世鹏
 * @Date 2021/4/12 10:50
 */
@Service
public class AnthologyTableServiceImpl implements AnthologyTableService {

    @Autowired
    private AnthologyTableMapper anthologyTableMapper;

    @Autowired
    private AnthologyTagsMapper anthologyTagsMapper;

    @Autowired
    private TagsTableMapper tagsTableMapper;

    @Autowired
    private FileAnthologyTableMapper fileAnthologyTableMapper;

    @Autowired
    private FileTableMapper fileTableMapper;

    @Override
    public String getList(HttpServletRequest req, HttpServletResponse resp, Integer state) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");
        if (id != null){
            AnthologyTable anthologyTable = new AnthologyTable();
            anthologyTable.setAnthologyId(id);
            if (state != null){
                anthologyTable.setState(state);
            }
            anthologyTable.setDelState(0);
            anthologyTable.setUserId(id);
            List<AnthologyTable> select = anthologyTableMapper.select(anthologyTable);
            for (AnthologyTable table : select) {
                String anthologyCoverPath = table.getAnthologyCoverPath();
                String realPath = req.getServletContext().getRealPath("/upload/img") + "/";
                table.setAnthologyCoverPath(realPath+anthologyCoverPath);
            }
            return MassageReturnUtils.toJson("0", "查询成功", select);
        }
        return MassageReturnUtils.toJson("-1", "未登录", null);
    }

    /**
     * 添加文集信息
     *
     * @param req            请求
     * @param resp           响应
     * @param anthologyTable 修改的文集信息
     * @return 成功true, 失败false
     */
    @Override
    public Boolean add(HttpServletRequest req, HttpServletResponse resp, AnthologyTable anthologyTable) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");

        if (id != null){
            anthologyTable.setUpTime(new Date());
            anthologyTable.setState(0);
            anthologyTable.setDelState(0);
            anthologyTable.setUserId(id);
            int insert = anthologyTableMapper.insert(anthologyTable);
            if (insert > 0) {
                Integer anthology = anthologyTable.getAnthologyId();
                //添加文件对应关系
                List<FileTable> fileTable = anthologyTable.getFileTable();
                if (fileTable != null){
                    //要求必须大于三个文档
                    if (fileTable.size() >= 3) {
                        for (FileTable table : fileTable) {
                            Integer fileId = table.getFileId();
                            FileTable fileTable1 = fileTableMapper.selectOne(table);
                            if (fileTable1 == null){
                                //文档不存在，无法创建文集
                                anthologyTableMapper.delete(anthologyTable);
                                return false;
                            }
                            FileAnthologyTable fileAnthologyTable1 = new FileAnthologyTable(fileId, anthology);
                            FileAnthologyTable fileAnthologyTable = fileAnthologyTableMapper.selectOne(fileAnthologyTable1);
                            if (fileAnthologyTable == null) {
                                //不存在对应关系，则将其加入
                                fileAnthologyTableMapper.insert(fileAnthologyTable1);
                            }
                        }
                    }else {
                        anthologyTableMapper.delete(anthologyTable);
                        return false;
                    }
                }else {
                    anthologyTableMapper.delete(anthologyTable);
                    return false;
                }
                //添加标签对应关系,Controller层已经保证所有标签都存在了
                List<TagsTable> tags = anthologyTable.getTags();
                if (tags != null){
                    for (TagsTable tag : tags) {
                        TagsTable tagsTable = tagsTableMapper.selectOne(tag);
                        //查询对应关系是否存在
                        AnthologyTags anthologyTags = new AnthologyTags(anthology, tagsTable.getTargsId());
                        AnthologyTags anthologyTags1 = anthologyTagsMapper.selectOne(anthologyTags);
                        //不存在则创建
                        if (anthologyTags1 == null) {
                            anthologyTagsMapper.insert(anthologyTags);
                        }
                    }
                }
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    /**
     * 下架 or 删除
     *
     * @param req         请求
     * @param resp        响应
     * @param anthologyId 文集ID
     */
    @Override
    public void delAnthology(HttpServletRequest req, HttpServletResponse resp, Integer anthologyId) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");
        AnthologyTable anthologyTable = new AnthologyTable();
        anthologyTable.setAnthologyId(anthologyId);
        AnthologyTable anthologyTable1 = anthologyTableMapper.selectOne(anthologyTable);
        if (anthologyTable1 != null && id != null && anthologyTable1.getUserId().equals(id)){
            anthologyTable1.setDelState(1);
            anthologyTableMapper.updateByPrimaryKey(anthologyTable1);

            //删除文档文集关系
            FileAnthologyTable fileAnthologyTable = new FileAnthologyTable();
            fileAnthologyTable.setAnthologyId(anthologyId);
            fileAnthologyTableMapper.delete(fileAnthologyTable);
            //删除标签文集关系
            AnthologyTags anthologyTags = new AnthologyTags();
            anthologyTags.setAnthologyId(anthologyId);
            anthologyTagsMapper.delete(anthologyTags);
        }
    }

    /**
     * 修改回显数据专用接口
     *
     * @param req         请求
     * @param resp        响应
     * @param anthologyId 文集ID
     * @return 回显的文集信息
     */
    @Override
    public AnthologyTable selectById(HttpServletRequest req, HttpServletResponse resp, Integer anthologyId) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");
        if (id != null){
            AnthologyTable anthologyTable1 = new AnthologyTable(anthologyId);
            anthologyTable1.setUserId(id);
            AnthologyTable anthologyTable = anthologyTableMapper.selectOne(anthologyTable1);
            String realPath = req.getServletContext().getRealPath("/upload/img") + "/";
            if (anthologyTable != null){
                //封面设置绝对地址
                String anthologyCoverPath = anthologyTable.getAnthologyCoverPath();
                anthologyTable.setAnthologyCoverPath(realPath+anthologyCoverPath);
            }
            return anthologyTable;
        }else {
            //没登陆
            return null;
        }
    }

    /**
     * 修改文集信息
     *
     * @param req            请求
     * @param resp           响应
     * @param anthologyTable 文集信息
     * @return 成功true, 失败false
     */
    @Override
    public Boolean update(HttpServletRequest req, HttpServletResponse resp, AnthologyTable anthologyTable) {
        //HttpSession session = req.getSession();
        Integer id = 1;//(Integer) session.getAttribute("id");
        if (id != null) {
            //登录了
            int insert = anthologyTableMapper.updateByPrimaryKey(anthologyTable);
            if (insert > 0) {
                Integer anthologyId = anthologyTable.getAnthologyId();
                List<TagsTable> tags = anthologyTable.getTags();
                if (tags != null){
                    for (TagsTable tag : tags) {
                        TagsTable tagsTable = tagsTableMapper.selectOne(tag);
                        AnthologyTags anthologyTags = new AnthologyTags(anthologyId,tagsTable.getTargsId());
                        AnthologyTags anthologyTags1 = anthologyTagsMapper.selectOne(anthologyTags);
                        if (anthologyTags1 == null) {
                            anthologyTagsMapper.insert(anthologyTags);
                        }
                    }
                }
                List<FileTable> fileTable = anthologyTable.getFileTable();
                if (fileTable != null) {
                    for (FileTable table : fileTable) {
                        FileTable fileTable1 = fileTableMapper.selectOne(table);
                        FileAnthologyTable fileAnthologyTable = new FileAnthologyTable(fileTable1.getFileId(), anthologyId);
                        FileAnthologyTable fileAnthologyTable1 = fileAnthologyTableMapper.selectOne(fileAnthologyTable);
                        if (fileAnthologyTable1 == null){
                            fileAnthologyTableMapper.insert(fileAnthologyTable);
                        }
                    }
                }
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    /**
     * 返回下载地址
     *
     * @param req  请求
     * @param resp 响应
     * @param id   文件ID
     * @return 下载地址集合
     */
    @Override
    public ArrayList<String> download(HttpServletRequest req, HttpServletResponse resp, Integer id) {
        List<FileAnthologyTable> select = fileAnthologyTableMapper.select(new FileAnthologyTable(id));
        ArrayList<String> arrayList = new ArrayList<>();
        if (select != null) {
            //把所有文件的路径放到集合中
            for (FileAnthologyTable fileAnthologyTable : select) {
                Integer fileId = fileAnthologyTable.getFileId();
                FileTable fileTable = fileTableMapper.selectOne(new FileTable(fileId));
                String filePath = fileTable.getFilePath();
                arrayList.add(filePath);
            }
        }
        return arrayList;
    }


}
